package com.example.im

import cc.vv.lkimcomponent.lkim.bean.LKIMMessage
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMChatType
import cc.vv.lkimcomponent.lkim.bean.venum.LKIMStatus
import cc.vv.lkimcomponent.lkim.operate.LKIMEdit
import com.example.util.finallist.IMExtKey
import com.example.util.finallist.SkKey

/**
 * 本地消息插入管理
 * @author  chaohaipeng  on 2018/7/23.
 */
class IMInsert {
    private object mHolder {
        val instance = IMInsert()
    }

    companion object {
        fun getInstance(): IMInsert {
            return mHolder.instance
        }
    }

    /**
     * 插入发送方文本消息
     *
     * @param account     会话id
     * @param content     文本内容
     * @param messageType 消息类型，类型详见IMExtKey.EXTKEY_MESSAGETYPE
     */
    fun sendSingleTxt(account: String, content: String, messageType: Int) {
        try {
            var txtSendMessage = LKIMMessage.createTxtSendMessage(content, account)
            txtSendMessage.setLKIMChatType(LKIMChatType.SingleChat)
            txtSendMessage.setLKIMStatus(LKIMStatus.SUCCESS)
            txtSendMessage = IMMessageInit.getInstance().initBaseExt(account, txtSendMessage, LKIMChatType.SingleChat)
            txtSendMessage.setAttribute(IMExtKey.EXTKEY_MESSAGETYPE, messageType.toString())
            LKIMEdit.getInstance().insert(account, txtSendMessage)
            /**
             * 通知聊天列表
             */
            IMReceiveService.getInstance().sendInsertCache(account, txtSendMessage, true)
            IMReceiveService.getInstance().sendRefreshConversation()

        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    /**
     * 插入接收方文本消息
     *
     * @param account     会话id
     * @param content     文本内容
     * @param messageType 消息类型，类型详见IMExtKey.EXTKEY_MESSAGETYPE
     */
    fun receiveSingleTxt(account: String, content: String, messageType: Int,type:String) {
        try {
            var txtReceiveMessage = LKIMMessage.createTxtReceiveMessage(content, account)
            txtReceiveMessage.setLKIMChatType(LKIMChatType.SingleChat)
            txtReceiveMessage = IMMessageInit.getInstance().initSingleReceiveBaseExt(account, txtReceiveMessage)
            txtReceiveMessage.setAttribute(IMExtKey.EXTKEY_MESSAGETYPE, messageType.toString())
            txtReceiveMessage.setAttribute(IMExtKey.EXTKEY_USERID, account)
            txtReceiveMessage.setAttribute(IMExtKey.EXTKEY_NOTIY_TYPE, type)
            LKIMEdit.getInstance().insert(account, txtReceiveMessage)
            /**
             * 通知聊天列表
             */
            IMReceiveService.getInstance().sendInsertCache(account, txtReceiveMessage, true)
            IMReceiveService.getInstance().sendRefreshConversation()
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }
    /**
     * 插入接收方文本消息
     *
     * @param account     会话id
     * @param content     文本内容
     * @param messageType 消息类型，类型详见IMExtKey.EXTKEY_MESSAGETYPE
     */
    fun receiveSingleTxt(account: String, content: String, messageType: Int, lkimChatType: LKIMChatType) {
        try {
            var txtReceiveMessage = LKIMMessage.createTxtReceiveMessage(content, account)
            txtReceiveMessage.setLKIMChatType(lkimChatType)
            txtReceiveMessage = IMMessageInit.getInstance().initSingleReceiveBaseExt(account, txtReceiveMessage)
            txtReceiveMessage.setAttribute(IMExtKey.EXTKEY_MESSAGETYPE, messageType.toString())
            txtReceiveMessage.setAttribute(IMExtKey.EXTKEY_USERID, account)
            LKIMEdit.getInstance().insert(account, txtReceiveMessage)
            /**
             * 通知聊天列表
             */
            IMReceiveService.getInstance().sendInsertCache(account, txtReceiveMessage, true)
            IMReceiveService.getInstance().sendRefreshConversation()
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }
    /**
     * 插入一条通知类型消息
     *
     * @param account      帐号
     * @param content      内容
     * @param msgTime      消息时间
     * @param lkimChatType 单聊群聊    LKIMChatType
     * @return 消息体
     */
    fun insertInformMessage(account: String, content: String, msgTime: Long, lkimChatType: LKIMChatType): LKIMMessage {
        val txtInformMessage = LKIMMessage.createTxtReceiveMessage(content, account)
        txtInformMessage.setLKIMChatType(lkimChatType)
        txtInformMessage.msgTime = msgTime
        txtInformMessage.setToAccount(account)
        txtInformMessage.setIsUnread(SkKey.BTKEY_String_0)
        txtInformMessage.setAttribute(IMExtKey.EXTKEY_MESSAGETYPE, SkKey.BTKEY_String_0)
        LKIMEdit.getInstance().insert(account, txtInformMessage)
        return txtInformMessage
    }
}